package com.myweb.demo.controller;

import com.myweb.demo.entity.Notice;
import com.myweb.demo.entity.Plate;
import com.myweb.demo.entity.Posts;
import com.myweb.demo.entity.User;
import com.myweb.demo.repository.*;
import javafx.geometry.Pos;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Controller
public class UserController {
    public UserController() {

    }

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private AdminRepository adminRepository;
    @Autowired
    private PlateRepository plateRepository;
    @Autowired
    PostsRepository postsRepository;
    @Autowired
    NoticeRepository noticeRepository;


    @GetMapping(value = "/user")
    @ResponseBody
    public List<User> getUser() {
        return userRepository.findAll();
    }

//    @GetMapping(value = "/user/{id}")
//    @ResponseBody
//    public Optional<User> getUser(@PathVariable(value = "id") Long id) {
//        return userRepository.findById(id);
//    }

    @RequestMapping(value = "/user/{id}",method = RequestMethod.POST)
    public ModelAndView userupdate(@RequestParam("username")String username,@RequestParam("userpassword")String userpassword,
                                   @RequestParam("usersex")String usersex,@RequestParam("useremail")String useremail,
                                   @RequestParam("userqq")String userqq,@RequestParam("userimg")String userimg,
                                   @PathVariable("id")Long userid,HttpServletRequest request,Model model){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        int userstatu = (int) session.getAttribute("userstatu");
        model.addAttribute("user", user);
        model.addAttribute("userstatu", userstatu);
        User olduser = userRepository.getUserByUserId(userid);
        userRepository.updateUser(username,usersex,userpassword,userqq,useremail,userimg,userid);
        return new ModelAndView("/user/"+userid);

    }

    @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
    public ModelAndView show_user(HttpServletRequest request,@PathVariable(name = "id")Long id,Model model) {
        int userstatu = 0;
        model.addAttribute("userstatu", userstatu);
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        User lookuser = userRepository.getUserByUserId(id);
        model.addAttribute("lookuser",lookuser);
        List<Posts> postsList = postsRepository.getPostsByUser(lookuser);
        model.addAttribute("posts_list",postsList);
        if (user != null) {
            userstatu = 1;
            session.setAttribute("userstatu", userstatu);
            model.addAttribute("user", user);
            model.addAttribute("userstatu", userstatu);
            return new ModelAndView("userinfo");
        }
        return new ModelAndView("userinfo");

    }

    @GetMapping(value = "/index")
    public ModelAndView webTitle(HttpServletRequest request,Model model) {
        int userstatu = 0;
        model.addAttribute("userstatu", userstatu);
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        List<Plate> platelist = plateRepository.findAll();
        model.addAttribute("platelist",platelist);
        List<Posts> question_posts = postsRepository.getQuestionPosts();
        model.addAttribute("question_posts",question_posts);
        List<Posts> hot_posts_list = postsRepository.getPostsOrderByDate();
        model.addAttribute("host_post_list",hot_posts_list);
        List<Plate> best_plate = plateRepository.getbestPost();
        model.addAttribute("best_plate",best_plate);
        List<Notice> notices = noticeRepository.findAll();
        model.addAttribute("notices",notices);
        if (user!=null) {
            userstatu = 1;
            session.setAttribute("userstatu",userstatu);
            model.addAttribute("user",user);
            model.addAttribute("userstatu", userstatu);
            return new ModelAndView("index", "userModel", model);
        }
        return new ModelAndView("index", "userModel", model);
    }
    @GetMapping("/register")
    public String toregister(){
        return "register";
    }
    @PostMapping("/register")
    public String register(HttpServletRequest request, @RequestParam(value = "username")String username, @RequestParam(value = "password")String password,
                           @RequestParam(value = "sex")String sex, @RequestParam(value = "email")String email, @RequestParam("qq")String qq){
        Date day=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        User user = new User(username,password,sex,email,df.format(day),0,qq,"img/default-touxiang.jpg",0,0);
        userRepository.saveAndFlush(user);
        return "/login";

    }

}